package com.demo.web.admin.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.demo.web.admin.config.properties.DubboProperties;

@Configuration
@ConditionalOnBean(value=DubboProperties.class) //条件: 表示当存在 DubboProperties bean 对象时才开始DubboConfig的相关bean配置; 类似的还有 AutoConfigureBefore,AutoConfigureAfter,ConditionalOnMissBean,Order等等
@DubboComponentScan(basePackages = "com.demo.web.admin")
public class DubboConfig {
	
	@Autowired
	private DubboProperties dubboProperties;
	
	@Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(dubboProperties.getAppName());
        applicationConfig.setLogger(dubboProperties.getLogger());
        return applicationConfig;
    }

    @Bean
    public ConsumerConfig consumerConfig() {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setCheck(false);//不检查dubbo服务是否可用
        consumerConfig.setTimeout(120000);
        return consumerConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(dubboProperties.getRegistryAddress());
        registryConfig.setProtocol("zookeeper");
        registryConfig.setClient("curator");
        return registryConfig;
    }
  
}  
